#include <QVBoxLayout>
#include <QFileDialog>
#include <QFile>
#include <QMessageBox>
#include <Configuration.h>
#include <Constants.h>
#include <MessageServices.h>
#include "IssuesBrowserWindow.h"
#include "IssuesToolBar.h"
#include "IssuesBrowser.h"

namespace Domy {

IssuesBrowserWindow::IssuesBrowserWindow(ActionEnableCtrl ctrl, QWidget *parent)
    : DockWidgetInterface(ctrl, parent),
      m_issues_bar(new IssuesToolBar(this)),
      m_issues_browser(new IssuesBrowser(this))
{
    QVBoxLayout *l = new QVBoxLayout(this);
    l->setMargin(0);
    l->setSpacing(0);
    l->addWidget(m_issues_bar, 0);
    l->addWidget(m_issues_browser, 1);
    this->setLayout(l);

    bool warning_visible = domy_config->getSettingsValue(SettingsGroup_Issues, SettingsKey_IssuesWarningVisible).toBool();
    bool error_visible = domy_config->getSettingsValue(SettingsGroup_Issues, SettingsKey_IssuesErrorVisible).toBool();

    m_issues_bar->setWaringVisibleActionChecked(warning_visible);
    m_issues_browser->setWarningIssuesListVisible(warning_visible);

    m_issues_bar->setErrorVisibleActionChecked(error_visible);
    m_issues_browser->setErrorIssuesListVisible(error_visible);

    connect(m_issues_bar, SIGNAL(actionTriggered(QAction *)),
            this, SLOT(slotOnIssuesActionTriggered(QAction *)));
    connect(domy_msg, SIGNAL(signalAppendIssues(const Issues &)),
            this, SLOT(slotOnAppendIssues(const Issues &)));
}

IssuesBrowserWindow::~IssuesBrowserWindow()
{

}

QDockWidget *IssuesBrowserWindow::createDockWidget()
{
    QDockWidget *dock_widget = new QDockWidget(tr("Issues"), parentWidget());
    dock_widget->setObjectName("IssuesBrowserWindow");
    dock_widget->setFeatures(dock_widget->features() & ~QDockWidget::DockWidgetFloatable);
    dock_widget->setWidget(this);

    return dock_widget;
}

void IssuesBrowserWindow::slotOnIssuesActionTriggered(QAction *action)
{
    IssuesActionKey key = static_cast<IssuesActionKey>(action->data().toInt());
    switch (key) {
    case IssuesAct_Export:
    {
        QString file_name = QFileDialog::getSaveFileName(this, tr("Export Issues"), domy_config->getLastOpenDirPath(), "*.txt");
        if (!file_name.isEmpty()) {
            QByteArray text = m_issues_browser->toByteArray();
            QFile file(file_name);
            bool suc = false;
            if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                qint64 len = file.write(text);
                suc = (len == text.length());
                file.close();
            }

            if (suc) {
                QMessageBox::information(this, tr("Export Issues"), tr("Export issues successfully !"),
                                         QMessageBox::Ok, QMessageBox::Ok);
            }
            else {
                QMessageBox::critical(this, tr("Export Issues"), tr("Export issues unsuccessfully !"),
                                      QMessageBox::Ok, QMessageBox::Ok);
            }
        }
    }
        break;
    case IssuesAct_Clear:
        m_issues_browser->clearIssues();
        break;
    case IssuesAct_Warning:
    {
        bool warning_visible = action->isChecked();
        m_issues_browser->setWarningIssuesListVisible(warning_visible);
        domy_config->setSettingsValue(SettingsGroup_Issues, SettingsKey_IssuesWarningVisible, warning_visible);
    }
        break;
    case IssuesAct_Error:
    {
        bool error_visible = action->isChecked();
        m_issues_browser->setErrorIssuesListVisible(error_visible);
        domy_config->setSettingsValue(SettingsGroup_Issues, SettingsKey_IssuesErrorVisible, error_visible);
    }
        break;
    default:
        break;
    }
}

void IssuesBrowserWindow::slotOnAppendIssues(const Issues &issues)
{
    switch (issues.type) {
    case IssuesType_Warning:
        m_issues_browser->appendWarningIssues(issues);
        break;
    case IssuesType_Error:
        m_issues_browser->appendErrorIssues(issues);
        break;
    default:
        break;
    }
}

} // namespace Domy
